package com.example.vue_shop.components.user.controller;

import com.example.vue_shop.components.user.entity.User;
import com.example.vue_shop.components.user.entity.UserPage;
import com.example.vue_shop.components.user.service.UserService;
import com.example.vue_shop.utils.ResultObj;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author lipengwei
 */
@Slf4j
@RestController
@Api(value = "用户管理", tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 测试接口，欢迎接口.
     *
     * @return ResultObj<String> 封装好的返回对象
     */
    @ApiOperation(value = "hello测试接口", notes = "这是一个测试接口，欢迎接口。")
    @GetMapping("/hello")
    public ResultObj<String> hello() {
        ResultObj<String> resultObj = new ResultObj<>();
        resultObj.setData("你好");
        log.info("进入hello测试接口！");
        return resultObj;
    }

    /**
     * 获取用户列表.
     */
    @ApiOperation("获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType =
                    "String"),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType =
                    "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的数量", dataType =
                    "int", example = "10")
    })
    @GetMapping("/users")
    public ResultObj<UserPage> getUserList(@RequestParam(required = false)
                                                   String username,
                                           @RequestParam(required = false, defaultValue = "1") int pageNum,
                                           @RequestParam(required = false,
                                                   defaultValue = "10") int pageSize) {
        ResultObj<UserPage> resultObj = new ResultObj<>();
        UserPage userPage = userService.getUserList(username, pageNum, pageSize);
        resultObj.setData(userPage);
        return resultObj;
    }


    /**
     * 更新用户状态.
     *
     * @param user 用户信息
     */
    @PutMapping("/users/state")
    @ApiOperation("更新用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户对象", dataType =
                    "User")
    })
    public ResultObj<String> updateUserState(@RequestBody User user) {
        ResultObj<String> resultObj = new ResultObj<>();

        try {
            userService.updateUserState(user);
            resultObj.setMessage("更新用户状态成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("更新用户状态失败！");
        }
        return resultObj;
    }

    /**
     * 新增用户.
     *
     * @param user 用户信息
     */
    @ApiOperation("新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户对象", dataType =
                    "User")
    })
    @PostMapping("/users/add")
    public ResultObj<String> addUser(@RequestBody User user) {
        ResultObj<String> resultObj = new ResultObj<>();
        try {
            userService.addUser(user);
            resultObj.setMessage("新增用户成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("新增用户失败！");
        }
        return resultObj;
    }

    /**
     * 根据用户Id获取用户信息
     *
     * @param id 用户Id
     */
    @ApiOperation("根据Id获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户Id", dataType =
                    "int")
    })
    @GetMapping("/users/getUserById")
    public ResultObj<User> getUserById(@RequestParam Long id) {
        ResultObj<User> resultObj = new ResultObj<>();
        try {
            User user = userService.getUserById(id);
            resultObj.setData(user);
            resultObj.setMessage("获取用户成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("获取用户失败！");
        }

        return resultObj;
    }

    /**
     * 更新密码.
     *
     * @param user       用户信息
     */
    @PutMapping("/users/edit")
    @ApiOperation("修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户对象", dataType =
                    "User")
    })
    public ResultObj<String> updatePassword(@RequestBody User user) {
        ResultObj<String> resultObj = new ResultObj<>();
        try {
            userService.updatePassword(user.getId(), user.getPassword());
            resultObj.setMessage("修改用户密码成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("修改用户密码失败！");
        }

        return resultObj;
    }

    /**
     * 删除用户.
     * @param id 用户id
     */
    @DeleteMapping("/users/del")
    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户对象", dataType =
                    "User")
    })
    public ResultObj<String> del(int id) {
        ResultObj<String> resultObj = new ResultObj<>();
        try {
            userService.del(id);
            resultObj.setMessage("删除用户成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setStatus(false);
            resultObj.setMessage("删除用户失败！");
        }
        return resultObj;
    }
}
